package com.example.myapplication.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.top;
import com.squareup.picasso.Picasso;

public class ItemListMainAdapter extends BaseAdapter {

    private List<top> objects = new ArrayList<top>();
    private Context context;
    private LayoutInflater layoutInflater;

    public ItemListMainAdapter(Context context,List<top> objects ) {
        this.objects=objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_list_main, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
        top top=objects.get(position);
        holder.tvTitle.setText(top.getTitle());
        String picUrl1=top.getImage1();
        String picUrl2=top.getImage2();
        String picUrl3=top.getImage3();

        if (!TextUtils.isEmpty(picUrl1)) {
            holder.iv1.setVisibility(View.VISIBLE);
            Picasso.get().load(picUrl1).into(holder.iv1);
        }else {
            holder.iv1.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(picUrl2)) {
            holder.iv2.setVisibility(View.VISIBLE);
            Picasso.get().load(picUrl2).into(holder.iv2);
        }else {
            holder.iv2.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(picUrl3)) {
            holder.iv3.setVisibility(View.VISIBLE);
            Picasso.get().load(picUrl3).into(holder.iv3);
        }else {
            holder.iv3.setVisibility(View.GONE);
        }


        holder.tvTime.setText(top.getTime());
        holder.tvSource.setText(top.getResoure());
        holder.imageDelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                objects.remove(position);
                notifyDataSetChanged();

            }
        });
        return convertView;
    }


    protected class ViewHolder {
        private TextView tvTitle;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private TextView tvTime;
    private TextView tvSource;
    private ImageView imageDelect;

        public ViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            iv1 = (ImageView) view.findViewById(R.id.iv_image1);
            iv2 = (ImageView) view.findViewById(R.id.iv_image2);
            iv3 = (ImageView) view.findViewById(R.id.iv_image3);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvSource = (TextView) view.findViewById(R.id.tv_source);
            imageDelect = (ImageView) view.findViewById(R.id.image_delect);
        }
    }

}
